#include "crc16.h"
#include <stm32f10x.h>
uint16_t CRC_161(uint8_t *data,uint16_t len)
        {

           int start=0;
           int i=0,j=0;
            uint16_t crc = 0;
           for(j=1;j<len;j++){
                uint8_t bt = data[j];
                for (i=0; i < 8; i++)
                {
                    bool b1 = (crc & 0x8000U) != 0;
                    bool b2 = (bt & 0x80U) != 0;
                    if (b1 != b2) crc = (uint16_t)((crc << 1) ^ 0x1021);
                    else crc <<= 1;
                    bt <<= 1;
                }
            }
            return crc;


        }


  int  check_data(uint8_t *buf,uint16_t len){
		
	        uint8_t *temp;
          uint16_t i;
          uint16_t index = 0;
	        uint32_t pack_len=0;
  
            if ((buf[0] == 0x55) && (buf[len - 1] == 0x55))
            {
                for(i=0;i<len;i++){
									
                        temp[index++] = buf[i];
			               	}
                    
            }
            else
            {
                return 1;
            }
						
					 pack_len|=temp[5];
					 pack_len<<=8;
					 pack_len|=temp[4];
					 pack_len<<=8;
					 pack_len|=temp[3];
					 pack_len<<=8;
		       pack_len|=temp[2];
	
            if (pack_len == (index - 4))
            {
                uint16_t crc = temp[index - 3];       
                 crc =(uint16_t)((crc << 8)|(uint16_t)temp[index - 2]);
                    if (crc == 0xffff)
                    {
                        return 0;    //test mode
                    }
                    else if (crc == CRC_161(temp,(uint16_t)(index- 3)))
                    {
                        return 0;
                    }
                    return 1;

               
            }
            return 1;
	
	
}
	
